package es.pawer.android.listacompras.data;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;

/**
 * This abstract class defines or implements the basic needed methods to complete a CRUD operations set.
 * 
 * @author plozano
 *
 * @param <E> is the Transfer Object class, which should be a plain JavaBean object.
 */
public abstract class AbstractLiteDAO<E extends ITransferObject> {
	
	/**
	 * This generic method makes a query without any constraint,
	 * so be careful using it.
	 * @return a list of transfer objects.
	 * @see es.pawer.android.listacompras.data.AbstractLiteDAO.createElement
	 */
	public List<E> getAll() {
		Cursor myCursor=MyDatabaseHelper.getWritableDB().query(
				getTableName(), null, null, null, null, null, null);
		return this.createList(myCursor);
		
	}
	
	/**
	 * Extracts the values of the Transfer Object to create a ContentValues Object.
	 * @param element The Transfer object.
	 * @return
	 * @throws DataException 
	 */
	protected abstract ContentValues getValues(E element) throws DataException;
	
	/**
	 * Looks up an element, searching by its primary key field.
	 * @param element a TO with a not-null PK attribute.
	 * @return the refreshed element if found. Otherwise it returns null
	 * @throws DataException 
	 */
	public abstract E getById(E element) throws DataException;
	
	/**
	 * Inserts a new element.
	 * @param element 
	 * @return The new element as persisted.
	 * @throws DataException 
	 */
	public long insert(E element) throws DataException {
		ContentValues values = getValues(element);
		long result=MyDatabaseHelper.getWritableDB().insert(getTableName(), null, values);
		if (result==-1) {
			throw new DataException("Element could not be inserted");
		} 
		return result;
		
	}
	
	/**
	 * Updates an existing element
	 * @param element
	 * @return the number of rows modified
	 * @throws DataException 
	 */
	public abstract int update (E element) throws DataException;
	
	/**
	 * Removes an element
	 * @param element the element to be removed
	 * @return the number of deleted rows on the table. 
	 * @throws DataException 
	 */
	public abstract int delete (E element) throws DataException;
	
	/**
	 * Removes elements from the table
	 * @param whereClause the conditions of the where clause without the WHERE keyword
	 * (i.e. "PRICE>? AND UNITS<?").
	 * @param values The values which will be inserted in the WHERE clause (i.e. {"400","50"}).
	 * @return the number of rows deleted.
	 */
	public int deleteBy(String whereClause,String[] values) {
		return MyDatabaseHelper.getWritableDB().delete(getTableName(), whereClause, values);
	}
	
	/**
	 * The name of the table whose rows are represented by T
	 * @return The given name
	 */
	public abstract String getTableName();
	
	/**
	 * This method creates a Transfer object from a Cursor row
	 * @param c the cursor object
	 * @param i the absolute index of the row
	 * @return a new Transfer Object
	 */
	protected abstract E obtainElement(Cursor c, int i);
	
	/**
	 * This method creates a list of transfer objects from a Cursor, using createElement
	 * @param c
	 * @return
	 */
	protected List<E> createList(Cursor c) {
		List<E> result= null;
		if (c.getCount()>0) {
			result= new ArrayList<E>();
			for (int i=0;i<c.getCount();i++) {
				result.add(this.obtainElement(c, i));
			}
		}
		return result;
	}
}
